/**
 * Created by fangjiahui on 15/12/11.
 */
define(function (require, exports, module) {
    require("/plugins/jQueryPlugins/jQueryLayer/layer.js");

    $.extend({
        showWarnDialogWithClose: function (msg, okFun, option) {
            var html = "<div class='tipsWrap w450'>" + "<div class='tipsTxt'>" + "<i class='iconfont tipIco'>" + "&#xe63c;" + "</i><span>" + msg + "</span></div>" + "<div class='tipsBtnBar'><a href='javascript:' class='_qd_dialog_ok btn1'>确定</a></div></div>"
            showDialog(1, html, okFun, option);
        },
        showWarnDialogWithOutClose: function (msg, okFun, option) {
            var html = "<div class='tipsWrap w450'>" + "<div class='tipsTxt'>" + "<i class='iconfont tipIco'>" + "&#xe63c;" + "</i><span>" + msg + "</span></div>" + "<div class='tipsBtnBar'><a href='javascript:' class='_qd_dialog_ok btn1'>确定</a></div></div>"
            showDialog(0, html, okFun, option);
        },
        showErrDialogWithClose: function (msg, okFun, option) {
            var html = '<div class="tipsWrap w384"><div class="tipsTxt"><i class="iconfont errIco">&#xe63e;</i><span>'+msg+'</span></div><div class="tipsBtnBar"><a href="javascript:" class="_qd_dialog_ok btn1">确定</a></div></div>';
            showDialog(1,html,okFun,option);
        },
        showErrDialogWithOutClose: function (msg, okFun, option) {
            var html = '<div class="tipsWrap w384"><div class="tipsTxt"><i class="iconfont errIco">&#xe63e;</i><span>' + msg + '</span></div><div class="tipsBtnBar"><a href="javascript:" class="_qd_dialog_ok btn1">确定</a></div></div>';
            showDialog(0, html, okFun, option);
        },
        showDialogNeedLogin: function (msg, okFun, option) {
            var html = '<div class="fileconfirm needLogin"><div class="tipsTxt"><i class="iconfont errIco">&#xe63e;</i><span>' + msg + '</span></div><div class="fileconfirmbtn"><a href="/user/login.html" class="btn1">重新登录</a><a class="cancleBtn _qd_dialog_cancel">取消</a></div></div>';
            showDialog(0, html, okFun, option);
        }
    });

    function showDialog(closeBtn, html, okFun, option) {
        option = option || {};
        var options = $.extend(option);
        options.dtype = option.type || 1;
        options.title = option.title || "信息提示:";
        options.area = option.area || ['450px', '190px'];
        options.border = option.border || [1, 1, '#cecfd0'];
        options.closeBtn = closeBtn;
        //构造确认框DOM
        var _qd_layer = window.layer.open({
            type: options.dtype,
            closeBtn: options.closeBtn,
            title: options.title,
            area: options.area,
            border: options.border,
            content:html
        });

        $("._qd_dialog_ok").unbind('click').click(function () {
            if (typeof(okFun) === "function") {
                if(okFun(_qd_layer)){
                    layer.close(_qd_layer);
                }
            } else {
                layer.close(_qd_layer);
            }
        });
        $("._qd_dialog_cancel").unbind('click').click(function () {
            layer.close(_qd_layer);
        });
    }

    exports.checkLogin = function (options) {
        if (!options) {
            return true;
        }
        else {
            if(options.result == false) {
                var errorCode = options.err_code || "";
                if(errorCode === "E0001") {
                    $.showDialogNeedLogin("您的帐户已在其它地方登录!如非本人操作请及时修改密码!");
                    return false;
                } /*else {
                    $.showErrDialogWithClose(options.msg);
                    return false;
                }*/
            }
        }
        return true;
    }


    //exports.tpl = $.fn.tpl;
    //exports.showDialogWithClose = $.showWarnDialogWithClose;
    //exports.showDialogWithOutClose = $.showWarnDialogWithOutClose;
    //exports.showErrDialogWithClose = $.showErrDialogWithClose;
    //exports.showErrDialogWithOutClose = $.showErrDialogWithOutClose;
    //
    //exports.showDialogNeedLogin = $.showDialogNeedLogin;
});